async function renderdom(data){

    response=await rendertable(data=data,domhtml='.students tbody',domdata=['id','username','password','age','gender','major','college','classname'])
 
    dataform['studentsdata']=data
    for (const i in data){
    $(`.students tbody tr:eq(${i})`).append(`<td><button class="layui-btn" id=${data[i]['id']} onclick=delstudent(this)>删除</button><button class="layui-btn layui-bg-blue" onclick="upstudent(this)" id=${data[i]['id']}>编辑</button></td>`)
  
}
}
!(async function(){
    layui.use(['layer', 'form', 'jquery'], async function(layero,a){
      
        let form=layui.form
        var layer = layui.layer;
      
        var $ = layui.jquery;
        let userData=await taskObject.fetchtemplate(path='/api/admin/students',method='GET')
        if (userData.code=200) {
            await renderdom(data=userData.data)
        }     
        let collegenamelist=[]
         for (const key of dataform.majordata){
            collegenamelist.push(key.collegename)
            
        }

        for (const i of Array.from(new Set(collegenamelist))){
           
            $('.students #studentsmajor').append(`<option value=${i}>${i}</option>`)
            form.render()
        }
     
        form.on('select(studentsmajor)', function(data){
           let value=data.value // 选中的值
           let formhtml='<option>选择班级</option>'
           let majorlist=[]
           for (const key of dataform.majordata){
            
               
               if (key.collegename==value){
                   majorlist.push(key.major)
                 
               }
               
           }
           for (const key of majorlist){
              for (const j of dataform.classinfodata1 ){
                  if (key==j.major){
                    formhtml+=`<option value="${j.classname}">${j.classname}</option>`
                  }
              }
           }
           $('#studentsclass').html(formhtml)
           
           form.render()
        });//搜索学生
    
        $('.students i').click(async (even)=>{
            await openAddUserDialog()
        },)

     
        
        form.on('submit(studentsform)',(data1)=>{ 
           
             let data2=JSON.parse(JSON.stringify(data1.field))
            Object.keys(data2).forEach((item)=>{
                if(data2[item]=='' && item!='username'){
                    layer.msg('请输入完整信息', {icon: 2,time:2000});
                    return falsestudentssearch
                }
            })

            !(async function(){         
                data2['collegename']=data2['major']
                response=await taskObject.fetchtemplate(path='/api/admin/studentssearch',method='POST',data=data2)
                if(response.code!=200){
                    layer.msg('网络错误',{icon:2,time:2000})
                    return false
                }
                studentdata=response.data
                await renderdom(data =JSON.parse(JSON.stringify(studentdata)))//渲染搜索内容
                return false
            }())
            return false;
        })      

        
        
    })
       

  
   

   

}())
async function delstudent(even){
    let valueId=parseInt(even.id);
    layer.confirm('确认删除?', {icon: 3}, async function(){
        response=await taskObject.fetchtemplate('/api/admin/students',method='DELETE',data={id:valueId})
        if(response.code==200){
            layer.msg('删除成功', {icon: 1,time:2000});
            setTimeout(()=>{
                window.location.reload();
            },1500)
            return false;
        }
        layer.msg(response.message, {icon: 2,time:2000});
      }, function(){
      
      });
}
async function openAddUserDialog() {
    layui.use('layer', function(){
        var layer = layui.layer;
        //班级信息 dataform.classinfodata1
        //
        let majorlisthtml=''
        let major1=''
        let collegenamelist=[]
        for (const key of dataform.majordata){
            //majorlisthtml+=`<option>${key.collegename}</option>`
            collegenamelist.push(key.collegename)
            let collegename=dataform.majordata[0].collegename
           
            if (key.collegename==collegename){
               
                major1+=`<option value="${key.major}">${key.major}</option>`
            }
        }
      
        for (const key1 of Array.from(new Set(collegenamelist))){
            majorlisthtml+=`<option>${key1}</option>`
        }
        var htmlContent = `
            <form class="layui-form">
                <div class="layui-form-item">
                    <label class="layui-form-label">姓名</label>
                    <div class="layui-input-block">
                        <input type="text" name="username" required lay-verify="required" placeholder="请输入姓名" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                <label class="layui-form-label">学院</label>
                <div class="layui-input-block">
                  <select lay-filter="collegelist" name="college">`+majorlisthtml+`</select>
                </div>
            </div>
               
               
                <div class="layui-form-item">
                    <label class="layui-form-label">专业</label>
                    <div class="layui-input-block">
                    <select lay-filter="majorlist" class="majorlist" name="major" ><option value="">选择专业</option>`+major1+`</select> </div>
                </div>
                <div class="layui-form-item">
                <label class="layui-form-label">班级</label>
                <div class="layui-input-block">
                <select lay-filter="classlist" class="classlist" name="classname"><option value="">选择班级</option></select> </div>
            </div>
            
              
                <div class="layui-form-item">
                <label class="layui-form-label">年龄</label>
                <div class="layui-input-block">
                    <input type="text" name="age" required lay-verify="required" class="layui-input">
                    
                </div>
            </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">性别</label>
                    <div class="layui-input-block">
                        <input type="radio" name="gender" value="男" title="男" checked>
                        <input type="radio" name="gender" value="女" title="女">
                    </div>
                </div>
               
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="formDemo">提交</button>
                    </div>
                </div>
            </form>
        `;

        // 使用 layer.open 弹出层
        layer.open({
            type: 1,
            title: '添加用户信息',
            area: ['500px', '400px'],
            content: htmlContent,
            btn: [],
            success: function(layero, index){
                let form=layui.form
                form.render()
                form.on('select(collegelist)',async (even)=>{
                    let value=even.value
                    let classinfohtml=''
                    for (const key of dataform.majordata){
                        if(value==key.collegename){
                            classinfohtml+='<option value="'+key.major+'">'+key.major+'</option>'
                        }
                    }
                    $('.majorlist').html(classinfohtml)
                    form.render()
                })
                form.on('select(majorlist)',async (even)=>{
                   
                    let value=even.value
                    let classinfohtml=''
                    for (const key of dataform.classinfodata1){
                        console.log(key);
                        if(value==key.major){
                            classinfohtml+='<option value="'+key.classname+'">'+key.classname+'</option>'
                        }
                    }
                    $('.classlist').html(classinfohtml)
                    form.render()
                })
                form.on('submit(formDemo)',function(data){
                    (async function(){
                        respose=await taskObject.fetchtemplate(path='/api/admin/students',method='POST',data=data.field)
                        if (respose.code==200){
                            layer.msg('添加成功',{icon:1,time:1200})
                            setTimeout(()=>{window.location.reload()},1500)
                            layer.close(index)
                        return ;
                        }else{
                            layer.msg(response.message,{icon:2,time:1200})
                        }
                    }())
                   

                    return false;
                })
               
            },
           
        });
    });
}


async function upstudent(even) {
    let valueid=parseInt(even.id);
    let userinfo=null
    console.log(dataform.studentsdata);
    dataform.studentsdata.forEach((item)=>{
        if(valueid==item.id){
            userinfo=item
        }
    })
    let uphtml = `<form class="layui-form">
    
    <div class="layui-form-item">
        <lable class="layui-form-lable">密码</lable>
        <div class="layui-input-block">
           <input type="password" class="layui-input" name="password" value="${userinfo.password}" required>
        </div>
       </div>
       <div class="layui-form-item">
        <lable class="layui-form-lable">年龄</lable>
        <div class="layui-input-block">
           <input type="text" class="layui-input" name="age" value="${userinfo.age}" required>
        </div>
       </div>
       <div class="layui-form-item">
        <button class="layui-btn layui-bg-blue" lay-submit lay-filter="studentsup">提交</button>
       </div>
    </form>`
    let layer=layui.layer;
    layer.open({
        title:"编辑学生",
        area: ['500px', '400px'],
        content: uphtml,
        btn: [],
        success: async function (layero, index) {
            let form=layui.form;
            form.render();
            form.on('submit(studentsup)',(data)=>{
                !(async function(){
                    data.field['userid']=valueid
                    response=await taskObject.fetchtemplate(path='/api/admin/students',method='PUT',data=data.field)
                   try{
                    if(response.code==200){
                        layer.msg('修改成功',{icon:1,time:1500})
                        setTimeout(()=>{
                          window.location.reload()
                        },1500)
                        return false
                    }else{
                        layer.msg(response.message,{icon:2,time:1500})
                        return false
                    }}catch(e){
                        layer.msg(e,{icon:4,time:3000})
                        return false
                    }
                  
                }())
                return false
            })
            return false
        }
    })
    
  }